package net.oschina.simpleplus.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import net.oschina.simpleplus.dao.UserDao;
import net.oschina.simpleplus.entity.User;

public class UserRealm extends AuthorizingRealm {

	@Autowired private UserDao userDao;
	
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		Object principal = token.getPrincipal();
		User user = userDao.selectByEmail((String) principal);
		if(user == null){
			throw new RuntimeException("");
		}
		return new SimpleAuthenticationInfo(principal, user.getPasswd(), ByteSource.Util.bytes(user.getEmail()+"_"+ user.getSalt()), getName());
	}


}
